<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_peserta_ujian extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
	
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.nim';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$tahun = isset($_POST['tahun']) ? strval($_POST['tahun']) : '';
		$kd_matakuliah = isset($_POST['kd_matakuliah']) ? strval($_POST['kd_matakuliah']) : '';
		$kd_dosen = isset($_POST['kd_dosen']) ? strval($_POST['kd_dosen']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('distinct a.id_krs, a.nim, b.nama, e.nama_matakuliah, e.sks, e.semester', false);
			$this->db->from('tbl_krs a');
			$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
			$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen','left');
			$this->db->join('tbl_krs_detail d', 'd.id_krs = a.id_krs','left');
			$this->db->join('ref_matakuliah e', 'e.kd_matakuliah = d.kd_matakuliah','left');
			$this->db->join('tbl_tahunajaran f', 'f.id_tahunajaran = a.id_tahunajaran','left');
			$this->db->join('ref_kurikulum g', 'g.kd_kurikulum = f.kd_kurikulum','left');
			$this->db->join('tbl_jadwal h', 'h.id_tahunajaran = f.id_tahunajaran ','left');
			
			$this->db->where('a.kd_fakultas', $kd_fakultas);
			$this->db->where('a.kd_prodi', $kd_prodi);
			$this->db->where('a.id_tahunajaran', $tahun);
			$this->db->where('d.kd_matakuliah', $kd_matakuliah);
			$this->db->where('h.kd_dosen', $kd_dosen);
			
			//$this->db->group_by('a.id_krs');
			//$this->db->group_by('d.kd_matakuliah');
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdata_detail($id){
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'nama_matakuliah';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_matakuliah, b.nama_matakuliah, b.sks, b.semester');
			$this->db->from('tbl_krs_detail a');
			$this->db->join('ref_matakuliah b', 'b.kd_matakuliah = a.kd_matakuliah');
			$this->db->where('a.id_krs', $id);
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		return json_encode($response);
	}
	
	function isExist_krs($data){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_krs a');
		
		$this->db->where('a.kd_fakultas', $data['kd_fakultas']);
		$this->db->where('a.kd_prodi', $data['kd_prodi']);
		$this->db->where('a.nim', $data['nim']);
		$this->db->where('a.id_tahunajaran', $data['id_tahunajaran']);
		
		$r = $this->db->get();
		if($r->num_rows() > 0){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	function getmatakuliah($kd_kurikulum, $kd_prodi, $id_tahunajaran){
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			//$this->db->select('kd_matakuliah, nama_matakuliah, sks, semester, kd_jenismk, space(0) as id_jadwal, space(0) as nama_dosen');
			$this->db->select("a.kd_matakuliah, a.nama_matakuliah, a.sks, a.semester, a.kd_jenismk, b.id_jadwal as id_jadwal, concat(b.kd_kelas, ' - ', c.nama_dosen) as nama_dosen, '0' as chk", false);
			$this->db->from('ref_matakuliah a');
			$this->db->join('tbl_jadwal b',"a.kd_matakuliah=b.kd_matakuliah and b.id_tahunajaran='$id_tahunajaran'",'left');
			$this->db->join('ref_dosen c','b.kd_dosen=c.kd_dosen','left');
			$this->db->where('a.kd_kurikulum', $kd_kurikulum);
			$this->db->where('a.kd_prodi', $kd_prodi);
			$this->db->where('a.ditawarkan', "Y");
			
			$this->db->group_by('a.kd_matakuliah');
			
			$this->db->order_by('a.semester' , 'ASC');
			$this->db->order_by('a.kd_matakuliah' , 'ASC');
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data		
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
		
	}
	
	function getsemester_mhs($nim){
		$this->db->flush_cache();
		$this->db->select('semester_mhsw');
		$this->db->where('nim', $nim);
		$this->db->where('semester !=', 'SP');
		$this->db->from('tbl_krs');
		$this->db->order_by('semester_mhsw', 'DESC');
		
		$r = $this->db->get();
		
		if($r->num_rows() > 0){
			return $r->row()->semester_mhsw + 1;
		}else{
			return 1;
		}
	}
	
	function getnomor(){
		$this->db->flush_cache();
		$query1 = $this->db->query("SHOW TABLE STATUS LIKE 'tbl_krs'");
		return $query1->row()->Auto_increment;
	}
	
	function InsertOnDb($data){
		$this->db->trans_start();
		
		# insert to krs
		$this->db->flush_cache();
        //$this->db->set('kd_pt', $data['kd_pt']);
        $this->db->set('kd_fakultas', $data['kd_fakultas']);
        $this->db->set('kd_prodi', $data['kd_prodi']);
        $this->db->set('nim', $data['nim']);
        $this->db->set('id_tahunajaran', $data['id_tahunajaran']);
		
		if($data['semester'] == 'SP'){
			$this->db->set('semester', 'SP');
			$this->db->set('semester_mhsw', '0');
		}else{
			$this->db->set('semester', substr($data['id_tahunajaran'], 4, 1) == 1?'Ganjil':'Genap');
			$this->db->set('semester_mhsw', $data['semester']);
		}
        
		$this->db->set('kd_dosen', $data['kd_dosen']);
        //$this->db->set('jum_sksk', $data['jum_sksk']);
        $this->db->set('ipk', '0');
        $this->db->set('jum_skss', $data['sks']);
        //$this->db->set('ips', $data['ips']);
        $this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
        //$this->db->set('status', 'Approve');
		
		$this->db->insert('tbl_krs');
		
		foreach($data['data']['rows'] as $row){
			if($row['chk'] == '1'){
				$this->db->flush_cache();
				$this->db->set('id_krs', $data['nomor']);
				$this->db->set('kd_matakuliah', $row['kd_matakuliah']);
				$this->db->set('id_jadwal', $row['id_jadwal']);
				$this->db->set('CUID', $this->session->userdata('user_id'));
				$this->db->set('CDATE', date('Y-m-d H:i:s'));
				
				$this->db->insert('tbl_krs_detail');
			}
		}
		
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
	function load_id_jadwal($id_tahun_ajaran, $kd_prodi, $kd_matakuliah){
		#get filter
		//$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id_jadwal, concat(a.kd_kelas, \' - \' ,b.nama_dosen) as nama_dosen', false);
			$this->db->from('tbl_jadwal a');
			$this->db->join('ref_dosen b', 'b.kd_dosen = a.kd_dosen');
			
			$this->db->where('a.id_tahunajaran', $id_tahun_ajaran);
			$this->db->where('a.kd_prodi', $kd_prodi);
			$this->db->where('a.kd_matakuliah', $kd_matakuliah);
			
			$this->db->order_by('b.nama_dosen', 'ASC');
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	/* ------------ KPRS --------------------------*/
	
	function getdata_krs($plimit=true){
		#get filter
		$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$id_tahunajaran = isset($_POST['id_tahunajaran']) ? strval($_POST['id_tahunajaran']) : '';
		$nim = isset($_POST['nim']) ? strval($_POST['nim']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id_krs, b.id_krs_detail, b.kd_matakuliah, c.nama_matakuliah, c.sks, c.semester, c.kd_jenismk');
			$this->db->from('tbl_krs a');
			$this->db->join('tbl_krs_detail b', 'b.id_krs = a.id_krs');
			$this->db->join('ref_matakuliah c', 'b.kd_matakuliah = c.kd_matakuliah');
			
			$this->db->where('a.kd_fakultas', $kd_fakultas);
			$this->db->where('a.kd_prodi', $kd_prodi);
			$this->db->where('a.id_tahunajaran', $id_tahunajaran);
			$this->db->where('a.nim', $nim);
			$this->db->where('b.status', 'A');
			
			//$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function delete_krs_detail($id){
		//query delet data  dengan ganti status db            
		$this->db->flush_cache();
		
		$this->db->set('status', "D");
	   
		$this->db->set('MUID', $this->session->userdata('user_id'));
		$this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_krs_detail', $id);
		$result = $this->db->update('tbl_krs_detail');
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}
	
	function isExist_detailkrs($data, &$id_krs_detail){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_krs a');
		$this->db->join('tbl_krs_detail b', 'b.id_krs = a.id_krs');
		
		$this->db->where('a.id_krs', $data['id_krs']);
		$this->db->where('a.kd_fakultas', $data['kd_fakultas']);
		$this->db->where('a.kd_prodi', $data['kd_prodi']);
		$this->db->where('a.nim', $data['nim']);
		$this->db->where('a.id_tahunajaran', $data['id_tahunajaran']);
		$this->db->where('b.kd_matakuliah', $data['kd_matakuliah']);
		
		$r = $this->db->get();
		if($r->num_rows() > 0){
			$id_krs_detail = $r->row()->id_krs_detail;
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	function update_status_detailkrs($id_krs_detail, $status){
		$this->db->flush_cache();
		$this->db->set('status', $status);
		$this->db->where('id_krs_detail', $id_krs_detail);
		return $this->db->update('tbl_krs_detail');
	}
	
	function insert_detailkrs($data){
		$this->db->flush_cache();
		$this->db->set('id_krs', $data['id_krs']);
		$this->db->set('kd_matakuliah', $data['kd_matakuliah']);
		$this->db->set('id_jadwal', $data['id_jadwal']);
		$this->db->set('CUID', $this->session->userdata('user_id'));
		$this->db->set('CDATE', date('Y-m-d H:i:s'));
		
		return $this->db->insert('tbl_krs_detail');
	}
	
	function get_krs($kode){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->start_cache();
			$this->db->select('a.id_krs, a.nim, b.id_krs_detail, b.kd_matakuliah, c.nama_matakuliah, c.sks, c.semester, c.kd_jenismk,e.nama,f.nama_dosen, g.nama_fakultas, h.nama_prodi');
			$this->db->from('tbl_krs a');
			$this->db->join('tbl_krs_detail b', 'b.id_krs = a.id_krs');
			$this->db->join('ref_matakuliah c', 'b.kd_matakuliah = c.kd_matakuliah');
			$this->db->join('tbl_mahasiswa e', 'e.nim = a.nim');
			$this->db->join('ref_dosen f', 'f.kd_dosen = a.kd_dosen');
			$this->db->join('ref_fakultas g', 'g.kd_fakultas = a.kd_fakultas');
			$this->db->join('ref_prodi h', 'h.kd_prodi = a.kd_prodi');
			//$this->db->join('ref_bobot u', 'u.kd_bobot = a.nilai_huruf');
			
			$this->db->where('a.id_krs', $kode);
			$this->db->where('b.status','A');
			//$this->db->group_by('b.kd_matakuliah');

		// proses
			$result = $this->db->get();
		
		 if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
		
	}
	
	function get_pdf_uts($fil,&$kd_matakuliah){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('distinct a.id_krs, a.nim, b.nama, e.nama_matakuliah, e.sks, e.semester, i.nama_fakultas, j.nama_prodi, f.Nama AS nama_tahunajaran, e.nama_matakuliah, c.nama_dosen, a.semester AS smstr, d.kd_matakuliah', false);
			
			$this->db->from('tbl_krs a');
			
			$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
			$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen');
			$this->db->join('tbl_krs_detail d', 'd.id_krs = a.id_krs');
			$this->db->join('ref_matakuliah e', 'e.kd_matakuliah = d.kd_matakuliah');
			$this->db->join('tbl_tahunajaran f', 'f.id_tahunajaran = a.id_tahunajaran');
			$this->db->join('ref_kurikulum g', 'g.kd_kurikulum = f.kd_kurikulum');
			$this->db->join('tbl_jadwal h', 'h.id_tahunajaran = f.id_tahunajaran ');
			$this->db->join('ref_fakultas i', 'i.kd_fakultas = a.kd_fakultas ');
			$this->db->join('ref_prodi j', 'j.kd_prodi = a.kd_prodi ');
			
			
			$this->db->where('a.kd_fakultas', $fil['kd_fakultas']);
			$this->db->where('a.kd_prodi', $fil['kd_prodi']);
			$this->db->where('a.id_tahunajaran', $fil['tahun']);
			$this->db->where('d.kd_matakuliah', $fil['kd_matakuliah']);
			$this->db->where('h.kd_dosen', $fil['kd_dosen']);
			//$this->db->where('a.semester', 'Ganjil');
			
			$this->db->order_by('a.nim');
			
			$this->db->group_by('a.nim');
	
		// proses
			$result = $this->db->get();
		
		 if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
		
	}
	
	function get_pdf_uas($fil,&$kd_matakuliah){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('distinct a.id_krs, a.nim, b.nama, e.nama_matakuliah, e.sks, e.semester, i.nama_fakultas, j.nama_prodi, f.Nama AS nama_tahunajaran, e.nama_matakuliah, c.nama_dosen, a.semester AS smstr, d.kd_matakuliah', false);
			
			$this->db->from('tbl_krs a');
			
			$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
			$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen');
			$this->db->join('tbl_krs_detail d', 'd.id_krs = a.id_krs');
			$this->db->join('ref_matakuliah e', 'e.kd_matakuliah = d.kd_matakuliah');
			$this->db->join('tbl_tahunajaran f', 'f.id_tahunajaran = a.id_tahunajaran');
			$this->db->join('ref_kurikulum g', 'g.kd_kurikulum = f.kd_kurikulum');
			$this->db->join('tbl_jadwal h', 'h.id_tahunajaran = f.id_tahunajaran ');
			$this->db->join('ref_fakultas i', 'i.kd_fakultas = a.kd_fakultas ');
			$this->db->join('ref_prodi j', 'j.kd_prodi = a.kd_prodi ');
			
			
			$this->db->where('a.kd_fakultas', $fil['kd_fakultas']);
			$this->db->where('a.kd_prodi', $fil['kd_prodi']);
			$this->db->where('a.id_tahunajaran', $fil['tahun']);
			$this->db->where('d.kd_matakuliah', $fil['kd_matakuliah']);
			$this->db->where('h.kd_dosen', $fil['kd_dosen']);
			//$this->db->where('a.semester', 'Ganjil');
			
			$this->db->order_by('a.nim');
			
			$this->db->group_by('a.nim');
	
		// proses
			$result = $this->db->get();
		
		 if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
		
	}
}

?>